package com.sunspring.zdcall.controller.commoncontroller;

import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.common.constant.ClientConstant;
import com.sunspring.zdcall.common.constant.enums.ClientEnum;
import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.dto.ClientDto;
import com.sunspring.zdcall.entity.ZdBaseClient;
import com.sunspring.zdcall.entity.ZdClientLogin;
import com.sunspring.zdcall.param.ClientParam;
import com.sunspring.zdcall.service.ClientService;
import com.sunspring.zdcall.support.cache.ClientCache;
import com.sunspring.zdcall.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * @author XJ
 * @date 2018-11-27
 */
@RestController
public class ClientController {

    @Autowired
    private ClientService clientService;

    @Autowired
    private SimpMessagingTemplate smt;

    /**
     * 根据参数查询客户端
     * @param clientParam ClientParam参数实体
     */
    @GetMapping("/sys/client/info")
    public SimpleResponse info(ClientParam clientParam) {
        return SimpleResponse.success(clientService.selectAll(clientParam));
    }

    /**
     * 根据MAC号查看客户端注册信息
     * @param mac 客户端mac号
     */
    @GetMapping("/client/myClient")
    public SimpleResponse myClient(String mac) {
        if (StringUtils.isBlank(mac)) {
            throw new ZdException("mac号不能为空");
        }
        ClientDto clientDto = clientService.getClientById(mac);
        if (clientDto == null) {
            return SimpleResponse.success(ClientConstant.CLIENT_NOT_REGISTER);
        }
        if (clientDto.getClientState() == StateEnum.registered.getCode()) {
            return SimpleResponse.success(ClientConstant.CLIENT_NOT_ACTIVATED);
        }
        if (clientDto.getClientState() == StateEnum.disable.getCode()) {
            return SimpleResponse.success(ClientConstant.CLIENT_DISABLED);
        }
        return SimpleResponse.success(clientDto);
    }

    /**
     * 注册客户端
     * @param clientParam ClientParam
     */
    @PostMapping("/client/register")
    public SimpleResponse register(@Valid ClientParam clientParam, BindingResult bindingResult) {
        //参数校验
        if (bindingResult.hasErrors()) {
            throw new ZdException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        clientService.register(clientParam);
        //客户端已注册，等待管理员激活
        return SimpleResponse.success(ClientConstant.CLIENT_NOT_ACTIVATED);
    }

    /**
     * 保存客户端配置参数
     * @param client ZdBaseClient参数实体
     */
    @PostMapping("/client/savePro")
    public SimpleResponse savePro(ZdBaseClient client) {
        clientService.saveProperties(client);
        //修改打印配置参数，通知前端
        if (client.getClientType() == ClientEnum.printer.getCode()) {
            Integer autoPrint = client.getAutoType();
            smt.convertAndSend("/printPro/" + client.getClientId(), autoPrint.toString());
        }
        return SimpleResponse.success();
    }


    /**
     * 停用，启用客户端
     * @param clientId 客户端ID
     */
    @PostMapping("/sys/client/switch")
    public SimpleResponse switchClient(String clientId) {
        clientService.switchClient(clientId);
        return SimpleResponse.success();
    }

    /**
     * 客户端启动，保存启动时间
     * @param clientId 客户端ID
     */
    @PostMapping("/client/startUp")
    public void clientStartUp(String clientId) {
        ClientCache.CLIENT_HOLDER.put(clientId,new Date());
    }

    /**
     * 客户端退出，记录日志
     * @param clientId 客户端ID
     */
    @PostMapping("/client/quit")
    public void clientQuit(String clientId) {
        Date startUpTime = ClientCache.CLIENT_HOLDER.get(clientId);
        if (startUpTime != null) {
            Date now = new Date();
            //统计在线时长
            float hours = (float) (Math.round(now.getTime() - startUpTime.getTime()) / 1000 / 360 / 10.0);
            ZdClientLogin zdClientLogin = new ZdClientLogin();
            zdClientLogin.setLoginClient(clientId);
            zdClientLogin.setLoginTime(startUpTime);
            zdClientLogin.setLogoutTime(now);
            zdClientLogin.setOnlineTime(hours);
            clientService.addClientLog(zdClientLogin);
            //移除登录时间
            ClientCache.CLIENT_HOLDER.remove(clientId);
        }
    }




}
